/*
 * drawPanel.java
 *
 * Created on October 13, 2004, 2:00 AM
 * Aug 30, 2007. The plot is no longer shown in an independent internalframe.
 */

package portfolio.stock.analize;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import javax.swing.*;
import javax.swing.JTabbedPane;
import java.util.Vector;
import portfolio.mainScreen;
import preprocess.*;
import misellanea.MiscellaneaDB;

/**
 *
 * @author  ACA
 */
public class DrawPanel implements ActionListener
{
    private JPanel         plotPanel;
    
    private JPanel         infoPanel;
    private JPanel         analysisPanel;
    private JMenuBar       toolBar;
    private JTabbedPane    m_tb;
    private JComboBox      TrainChoice;
    private JLabel         TrainLabel;
    private em_graph       graphPanel;
    private DisplayResults  dataPanel;
    private mainScreen     fatherFrame;

    private double [][]    inputData;
    private Vector         vTableHeader;
    private String         sStockName;
    private int            iTerm;
    private int            iTrainLen;
    private Vector         vTimeLine = null;
    private Preprocess     preprocess;
    private JTextField     fileText;
    /**
     * This method is called when the button is pressed                
     */
    private class ActAdapter implements ActionListener
    {
        private DrawPanel refPanel;
        /**
         *
         */
        ActAdapter (DrawPanel refPanelParam)//int iCommandID, StocksMenu inputMenu)
        {
            refPanel = refPanelParam;
            //m_commandID = iCommandID;
            //fatherMenu  = inputMenu;
        } /* End Constructor portfolioMainScr::stocksMenu::ActAdapter */
        /**
         *
         */
        public void actionPerformed (ActionEvent evt) {
                            // Perform action
            //
            String sAcctionCommand = evt.getActionCommand ();
            if (sAcctionCommand == "Cancel")
//ACA                refPanel.dispose ();
                ;
            if (sAcctionCommand == "Save") {
            // Create an image to save
                try {
                    //RenderedImage rendImage = graphPanel.drawJpgGraphic (700, 500);
                    //PortfolioDB portfolioDB = fatherFrame.getPortfolioDB ();
                    Connection connection = fatherFrame.getDBLoggedHost().getConnection();
                    double [] dReturnRisk = MiscellaneaDB.insertStock2Pflio(connection, sStockName,  vTimeLine,  inputData, iTerm);
                    //fatherFrame.showInsertedStock (sStockName, dReturnRisk);
/*0j0*/             double dReturn = dReturnRisk [0];
/*0j0*/             double dRisk   = dReturnRisk [1];
/*0j0*/             fatherFrame.displayMessage ("DrawPanel.actionPerformed 87", sStockName+ " risk: " + dRisk + " return " + dReturn + ".");
                } catch (Exception e) {
                    //e.printStackTrace();
                    fatherFrame.displayMessage ("DrawPanel.actionPerformed 90", "DrawPanel::ActAdapter::actionPerformed" + e);
                    return;
                }
                // Write generated image to a file
//0j0                try {
                    // Save as JPEG
                    String sFileText = fileText.getText ();
                    if (sFileText == null)
                        sFileText = "Temp";
                    
//0j0                    RenderedImage rendImage = graphPanel.drawJpgGraphic (700, 500);
//0j0                    File file = new File("experiments/" + fileText.getText() + ".jpg");
//0j0                    ImageIO.write(rendImage, "jpg", file);
//0j0                    
//0j0                    FileWriter fw = new FileWriter("experiments/" + fileText.getText() +".html");
//0j0                    dataPanel.setImageName (sFileText);
//0j0                    JTextArea txtJta = dataPanel.getTextAreaHandle ();
//0j0                    txtJta.write (fw);
//0j0                    fw.close();
//0j0                } catch (IOException e) {
//0j0                    e.printStackTrace();
//0j0                    JOptionPane.showMessageDialog (null, "DrawPanel::ActAdapter::actionPerformed" + e);
//0j0                    return;
//0j0                }  
            }

        } /* End Method portfolioMainScr::stocksMenu::ActAdapter::actionPerformed */
    } /* End class portfolioMainScr::stocksMenu::ActAdapter */
    /**
     *
     */
    public DrawPanel(String        sStockParam,
                    double[][]     inputDataParam,
                    Vector         vHeaderParam,
                    int            iTermParam,
                    int            iTrainLenParam,
                    Vector         vTimeLineParam,
                    int            iSideX, 
                    int            iSideY, 
                    mainScreen     mainFrame, 
                    String         sLabel, 
                    String         sTabName,
                    Preprocess     preproParam) 
    {
        int iTrainChoice = 0;

        inputData      = inputDataParam;
        vTableHeader   = vHeaderParam;
        fatherFrame    = mainFrame;
        sStockName     = sStockParam;
        iTerm          = iTermParam;
        iTrainLen      = iTrainLenParam;
        vTimeLine      = vTimeLineParam;
        preprocess     = preproParam;

        plotPanel = fatherFrame.getPlotPanel();
        plotPanel.setLayout(new BorderLayout ());

        GridBagLayout gridBag = new GridBagLayout ();
        infoPanel = new JPanel (gridBag) {
            public Insets getInsets ()
            {return new Insets (10,5, 10, 5);};
        };
        infoPanel.setLayout (new BoxLayout (infoPanel, BoxLayout.Y_AXIS));
        plotPanel.add(infoPanel, BorderLayout.CENTER);
        
        GridBagConstraints constraints = new GridBagConstraints ();
        constraints.gridx   = 1;
        constraints.gridy   = 1;
        constraints.anchor  = GridBagConstraints.WEST;
        constraints.weightx = 1;
        constraints.weighty = 1;
        
        graphPanel = new em_graph (inputData, 
                                   vTimeLine, 
                                   iTerm, 
                                   iTrainLen, 
                                   iSideX, 
                                   iSideY, 
                                   sTabName);
        infoPanel.add(graphPanel);
        
        JPanel buttonArea = new JPanel ();
        buttonArea.setLayout (new FlowLayout (FlowLayout.LEFT));
        plotPanel.add(buttonArea, BorderLayout.SOUTH);

        ActionListener actAdapter = new ActAdapter (this);
        
        JButton cancelButton = new JButton ("Cancel");
        buttonArea.add (cancelButton);
        cancelButton.addActionListener (actAdapter);
        
        JButton saveButton = new JButton ("Save");
        buttonArea.add (saveButton);
        saveButton.addActionListener (actAdapter);
        
        buttonArea.add (new JLabel ("  File: "));
	fileText = new JTextField (sStockName, 15);
        buttonArea.add (fileText);           

//ACA        plotPanel.show ();
    }
    /**
     *
     */
    public void actionPerformed (ActionEvent e)
    {
        plotPanel.repaint();
        graphPanel.repaint ();
    }
} /* End Class drawPanel */